<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: Slicing and Indexing</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('group__TutorialSlicingIndexing.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Slicing and Indexing<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__chapter.html">Dense matrix and array manipulation</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<p>This page presents the numerous possibilities offered by <code>operator()</code> to index sub-set of rows and columns. This API has been introduced in Eigen 3.4. It supports all the feature proposed by the <a class="el" href="group__TutorialBlockOperations.html">block API </a>, and much more. In particular, it supports <b>slicing</b> that consists in taking a set of rows, columns, or elements, uniformly spaced within a matrix or indexed from an array of indices.</p>
<h1><a class="anchor" id="TutorialSlicingOverview"></a>
Overview</h1>
<p>All the aforementioned operations are handled through the generic DenseBase::operator()(const RowIndices&amp;, const ColIndices&amp;) method. Each argument can be:</p><ul>
<li>An integer indexing a single row or column, including symbolic indices.</li>
<li>The symbol Eigen::all representing the whole set of respective rows or columns in increasing order.</li>
<li>An <a class="el" href="classEigen_1_1ArithmeticSequence.html">ArithmeticSequence</a> as constructed by the <a class="el" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">Eigen::seq</a>, <a class="el" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">Eigen::seqN</a>, or Eigen::placeholders::lastN functions.</li>
<li>Any 1D vector/array of integers including Eigen's vector/array, expressions, std::vector, std::array, as well as plain C arrays: <code>int[N]</code>.</li>
</ul>
<p>More generally, it can accepts any object exposing the following two member functions: </p><div class="fragment"><div class="line">&lt;integral type&gt; operator[](&lt;integral type&gt;) <span class="keyword">const</span>;</div>
<div class="line">&lt;integral type&gt; size() <span class="keyword">const</span>;</div>
</div><!-- fragment --><p> where <code>&lt;integral type&gt;</code> stands for any integer type compatible with <a class="el" href="namespaceEigen.html#a62e77e0933482dafde8fe197d9a2cfde" title="The Index type as used for the API.">Eigen::Index</a> (i.e. <code>std::ptrdiff_t</code>).</p>
<h1><a class="anchor" id="TutorialSlicingBasic"></a>
Basic slicing</h1>
<p>Taking a set of rows, columns, or elements, uniformly spaced within a matrix or vector is achieved through the <a class="el" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">Eigen::seq</a> or <a class="el" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">Eigen::seqN</a> functions where "seq" stands for arithmetic sequence. Their signatures are summarized below:</p>
<table class="manual">
<tr>
<th>function </th><th>description </th><th>example  </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(firstIdx,lastIdx) </div>
<div class="ttc" id="anamespaceEigen_html_a0c04400203ca9b414e13c9c721399969"><div class="ttname"><a href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">Eigen::seq</a></div><div class="ttdeci">auto seq(FirstType f, LastType l, IncrType incr)</div></div>
</div><!-- fragment --> </td><td>represents the sequence of integers ranging from <code>firstIdx</code> to <code>lastIdx</code>  </td><td><div class="fragment"><div class="line"><a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(2,5) &lt;=&gt; {2,3,4,5} </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(firstIdx,lastIdx,incr) </div>
</div><!-- fragment --> </td><td>same but using the increment <code>incr</code> to advance from one index to the next </td><td><div class="fragment"><div class="line"><a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(2,8,2) &lt;=&gt; {2,4,6,8} </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">seqN</a>(firstIdx,size) </div>
<div class="ttc" id="anamespaceEigen_html_a3a3c346d2a61d1e8e86e6fb4cf57fbda"><div class="ttname"><a href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">Eigen::seqN</a></div><div class="ttdeci">ArithmeticSequence&lt; typename internal::cleanup_index_type&lt; FirstType &gt;::type, typename internal::cleanup_index_type&lt; SizeType &gt;::type, typename internal::cleanup_seq_incr&lt; IncrType &gt;::type &gt; seqN(FirstType first, SizeType size, IncrType incr)</div><div class="ttdef"><b>Definition:</b> ArithmeticSequence.h:99</div></div>
</div><!-- fragment --> </td><td>represents the sequence of <code>size</code> integers starting from <code>firstIdx</code>  </td><td><div class="fragment"><div class="line"><a class="code" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">seqN</a>(2,5) &lt;=&gt; {2,3,4,5,6} </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">seqN</a>(firstIdx,size,incr) </div>
</div><!-- fragment --> </td><td>same but using the increment <code>incr</code> to advance from one index to the next </td><td><div class="fragment"><div class="line"><a class="code" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">seqN</a>(2,3,3) &lt;=&gt; {2,5,8} </div>
</div><!-- fragment -->  </td></tr>
</table>
<p>The <code>firstIdx</code> and <code>lastIdx</code> parameters can also be defined with the help of the Eigen::last symbol representing the index of the last row, column or element of the underlying matrix/vector once the arithmetic sequence is passed to it through operator(). Here are some examples for a 2D array/matrix <code>A</code> and a 1D array/vector <code>v</code>. </p><table class="manual">
<tr>
<th>Intent </th><th>Code </th><th>Block-API equivalence  </th></tr>
<tr>
<td>Bottom-left corner starting at row <code>i</code> with <code>n</code> columns </td><td><div class="fragment"><div class="line">A(<a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(i,<a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>), <a class="code" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">seqN</a>(0,n)) </div>
<div class="ttc" id="agroup__Core__Module_html_ga66661a473fe06e47e3fd5c591b6ffe8d"><div class="ttname"><a href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">Eigen::placeholders::last</a></div><div class="ttdeci">static const last_t last</div><div class="ttdef"><b>Definition:</b> IndexedViewHelper.h:44</div></div>
</div><!-- fragment --> </td><td><div class="fragment"><div class="line">A.bottomLeftCorner(A.rows()-i,n) </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td>Block starting at <code>i</code>,j having <code>m</code> rows, and <code>n</code> columns </td><td><div class="fragment"><div class="line">A(<a class="code" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">seqN</a>(i,m), <a class="code" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">seqN</a>(i,n)) </div>
</div><!-- fragment --> </td><td><div class="fragment"><div class="line">A.block(i,j,m,n) </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td>Block starting at <code>i0</code>,j0 and ending at <code>i1</code>,j1 </td><td><div class="fragment"><div class="line">A(<a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(i0,i1), <a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(j0,j1) </div>
</div><!-- fragment --> </td><td><div class="fragment"><div class="line">A.block(i0,j0,i1-i0+1,j1-j0+1) </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td>Even columns of A </td><td><div class="fragment"><div class="line">A(<a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">all</a>, <a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(0,<a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>,2)) </div>
<div class="ttc" id="agroup__Core__Module_html_ga4abe6022fbef6cda264ef2947a2be1a9"><div class="ttname"><a href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">Eigen::placeholders::all</a></div><div class="ttdeci">static const Eigen::internal::all_t all</div><div class="ttdef"><b>Definition:</b> IndexedViewHelper.h:189</div></div>
</div><!-- fragment --> </td><td></td></tr>
<tr>
<td>First <code>n</code> odd rows A </td><td><div class="fragment"><div class="line">A(<a class="code" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">seqN</a>(1,n,2), <a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">all</a>) </div>
</div><!-- fragment --> </td><td></td></tr>
<tr>
<td>The last past one column </td><td><div class="fragment"><div class="line">A(<a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">all</a>, <a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>-1) </div>
</div><!-- fragment --> </td><td><div class="fragment"><div class="line">A.col(A.cols()-2) </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td>The middle row </td><td><div class="fragment"><div class="line">A(<a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>/2,<a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">all</a>) </div>
</div><!-- fragment --> </td><td><div class="fragment"><div class="line">A.row((A.rows()-1)/2) </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td>Last elements of v starting at i </td><td><div class="fragment"><div class="line">v(<a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(i,<a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>)) </div>
</div><!-- fragment --> </td><td><div class="fragment"><div class="line">v.tail(v.size()-i) </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td>Last <code>n</code> elements of v </td><td><div class="fragment"><div class="line">v(<a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(<a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>+1-n,<a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>)) </div>
</div><!-- fragment --> </td><td><div class="fragment"><div class="line">v.tail(n) </div>
</div><!-- fragment -->  </td></tr>
</table>
<p>As seen in the last example, referencing the <em> last n </em> elements (or rows/columns) is a bit cumbersome to write. This becomes even more tricky and error prone with a non-default increment. Here comes <a class="el" href="">Eigen::placeholders::lastN(size) </a>, and <a class="el" href="">Eigen::placeholders::lastN(size,incr) </a>:</p>
<table class="manual">
<tr>
<th>Intent </th><th>Code </th><th>Block-API equivalence  </th></tr>
<tr>
<td>Last <code>n</code> elements of v </td><td><div class="fragment"><div class="line">v(lastN(n)) </div>
</div><!-- fragment --> </td><td><div class="fragment"><div class="line">v.tail(n) </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td>Bottom-right corner of A of size <code>m</code> times <code>n</code>  </td><td><div class="fragment"><div class="line">v(lastN(m), lastN(n)) </div>
</div><!-- fragment --> </td><td><div class="fragment"><div class="line">A.bottomRightCorner(m,n) </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td>Bottom-right corner of A of size <code>m</code> times <code>n</code>  </td><td><div class="fragment"><div class="line">v(lastN(m), lastN(n)) </div>
</div><!-- fragment --> </td><td><div class="fragment"><div class="line">A.bottomRightCorner(m,n) </div>
</div><!-- fragment -->  </td></tr>
<tr>
<td>Last <code>n</code> columns taking 1 column over 3 </td><td><div class="fragment"><div class="line">A(<a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">all</a>, lastN(n,3)) </div>
</div><!-- fragment --> </td><td></td></tr>
</table>
<h1><a class="anchor" id="TutorialSlicingFixed"></a>
Compile time size and increment</h1>
<p>In terms of performance, Eigen and the compiler can take advantage of compile-time size and increment. To this end, you can enforce compile-time parameters using <a class="el" href="group__Core__Module.html#gac01f234bce100e39e6928fdc470e5194">Eigen::fix&lt;val&gt;</a>. Such compile-time value can be combined with the Eigen::last symbol: </p><div class="fragment"><div class="line">v(<a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(<a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>-fix&lt;7&gt;, <a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>-fix&lt;2&gt;))</div>
</div><!-- fragment --><p> In this example Eigen knowns at compile-time that the returned expression has 6 elements. It is equivalent to: </p><div class="fragment"><div class="line">v(<a class="code" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">seqN</a>(<a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>-7, fix&lt;6&gt;))</div>
</div><!-- fragment --><p>We can revisit the <em>even columns of A</em> example as follows: </p><div class="fragment"><div class="line">A(<a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">all</a>, <a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(0,<a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>,fix&lt;2&gt;))</div>
</div><!-- fragment --><h1><a class="anchor" id="TutorialSlicingReverse"></a>
Reverse order</h1>
<p>Row/column indices can also be enumerated in decreasing order using a negative increment. For instance, one over two columns of A from the column 20 to 10: </p><div class="fragment"><div class="line">A(<a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">all</a>, <a class="code" href="namespaceEigen.html#a0c04400203ca9b414e13c9c721399969">seq</a>(20, 10, fix&lt;-2&gt;))</div>
</div><!-- fragment --><p> The last <code>n</code> rows starting from the last one: </p><div class="fragment"><div class="line">A(<a class="code" href="namespaceEigen.html#a3a3c346d2a61d1e8e86e6fb4cf57fbda">seqN</a>(<a class="code" href="group__Core__Module.html#ga66661a473fe06e47e3fd5c591b6ffe8d">last</a>, n, fix&lt;-1&gt;), <a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">all</a>)</div>
</div><!-- fragment --><p> You can also use the ArithmeticSequence::reverse() method to reverse its order. The previous example can thus also be written as: </p><div class="fragment"><div class="line">A(lastN(n).reverse(), <a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">all</a>)</div>
</div><!-- fragment --><h1><a class="anchor" id="TutorialSlicingArray"></a>
Array of indices</h1>
<p>The generic <code>operator()</code> can also takes as input an arbitrary list of row or column indices stored as either an <code>ArrayXi</code>, a <code>std::vector&lt;int&gt;</code>, <code>std::array&lt;int,N&gt;</code>, etc.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line">std::vector&lt;int&gt; ind{4,2,5,5,3};</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#gaceba5f63d4040c79c44109590d4fa606">MatrixXi</a> A = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">MatrixXi::Random</a>(4,6);</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Initial matrix A:\n&quot;</span> &lt;&lt; A &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;A(all,ind):\n&quot;</span> &lt;&lt; A(<a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">Eigen::placeholders::all</a>,ind) &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_ae814abb451b48ed872819192dc188c19"><div class="ttname"><a href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">Eigen::DenseBase::Random</a></div><div class="ttdeci">static const RandomReturnType Random()</div><div class="ttdef"><b>Definition:</b> Random.h:114</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_gaceba5f63d4040c79c44109590d4fa606"><div class="ttname"><a href="group__matrixtypedefs.html#gaceba5f63d4040c79c44109590d4fa606">Eigen::MatrixXi</a></div><div class="ttdeci">Matrix&lt; int, Dynamic, Dynamic &gt; MatrixXi</div><div class="ttdoc">Dynamic×Dynamic matrix of type int.</div><div class="ttdef"><b>Definition:</b> Matrix.h:499</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">Initial matrix A:
  7   9  -5  -3   3 -10
 -2  -6   1   0   5  -5
  6  -3   0   9  -8  -8
  6   6   3   9   2   6

A(all,ind):
  3  -5 -10 -10  -3
  5   1  -5  -5   0
 -8   0  -8  -8   9
  2   3   6   6   9

</pre> </td></tr>
</table>
<p>You can also directly pass a static array: </p><table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><a class="code" href="group__matrixtypedefs.html#gaceba5f63d4040c79c44109590d4fa606">MatrixXi</a> A = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">MatrixXi::Random</a>(4,6);</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Initial matrix A:\n&quot;</span> &lt;&lt; A &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;A(all,{4,2,5,5,3}):\n&quot;</span> &lt;&lt; A(<a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">Eigen::placeholders::all</a>,{4,2,5,5,3}) &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">Initial matrix A:
  7   9  -5  -3   3 -10
 -2  -6   1   0   5  -5
  6  -3   0   9  -8  -8
  6   6   3   9   2   6

A(all,{4,2,5,5,3}):
  3  -5 -10 -10  -3
  5   1  -5  -5   0
 -8   0  -8  -8   9
  2   3   6   6   9

</pre> </td></tr>
</table>
<p>or expressions: </p><table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line">ArrayXi ind(5); ind&lt;&lt;4,2,5,5,3;</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#gaceba5f63d4040c79c44109590d4fa606">MatrixXi</a> A = <a class="code" href="classEigen_1_1DenseBase.html#ae814abb451b48ed872819192dc188c19">MatrixXi::Random</a>(4,6);</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Initial matrix A:\n&quot;</span> &lt;&lt; A &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;A(all,ind-1):\n&quot;</span> &lt;&lt; A(<a class="code" href="group__Core__Module.html#ga4abe6022fbef6cda264ef2947a2be1a9">Eigen::placeholders::all</a>,ind-1) &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
</div><!-- fragment -->  </td><td><pre class="fragment">Initial matrix A:
  7   9  -5  -3   3 -10
 -2  -6   1   0   5  -5
  6  -3   0   9  -8  -8
  6   6   3   9   2   6

A(all,ind-1):
-3  9  3  3 -5
 0 -6  5  5  1
 9 -3 -8 -8  0
 9  6  2  2  3

</pre> </td></tr>
</table>
<p>When passing an object with a compile-time size such as <code>Array4i</code>, <code>std::array&lt;int,N&gt;</code>, or a static array, then the returned expression also exhibit compile-time dimensions.</p>
<h1><a class="anchor" id="TutorialSlicingCustomArray"></a>
Custom index list</h1>
<p>More generally, <code>operator()</code> can accept as inputs any object <code>ind</code> of type <code>T</code> compatible with: </p><div class="fragment"><div class="line"><a class="code" href="namespaceEigen.html#a62e77e0933482dafde8fe197d9a2cfde">Index</a> s = ind.size(); or <a class="code" href="namespaceEigen.html#a62e77e0933482dafde8fe197d9a2cfde">Index</a> s = size(ind);</div>
<div class="line"><a class="code" href="namespaceEigen.html#a62e77e0933482dafde8fe197d9a2cfde">Index</a> i;</div>
<div class="line">i = ind[i];</div>
<div class="ttc" id="anamespaceEigen_html_a62e77e0933482dafde8fe197d9a2cfde"><div class="ttname"><a href="namespaceEigen.html#a62e77e0933482dafde8fe197d9a2cfde">Eigen::Index</a></div><div class="ttdeci">EIGEN_DEFAULT_DENSE_INDEX_TYPE Index</div><div class="ttdoc">The Index type as used for the API.</div><div class="ttdef"><b>Definition:</b> Meta.h:59</div></div>
</div><!-- fragment --><p>This means you can easily build your own fancy sequence generator and pass it to <code>operator()</code>. Here is an example enlarging a given matrix while padding the additional first rows and columns through repetition:</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="keyword">struct </span>pad {</div>
<div class="line">  <a class="code" href="namespaceEigen.html#a62e77e0933482dafde8fe197d9a2cfde">Index</a> size()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> out_size; }</div>
<div class="line">  <a class="code" href="namespaceEigen.html#a62e77e0933482dafde8fe197d9a2cfde">Index</a> operator[] (<a class="code" href="namespaceEigen.html#a62e77e0933482dafde8fe197d9a2cfde">Index</a> i)<span class="keyword"> const </span>{ <span class="keywordflow">return</span> std::max&lt;Index&gt;(0,i-(out_size-in_size)); }</div>
<div class="line">  <a class="code" href="namespaceEigen.html#a62e77e0933482dafde8fe197d9a2cfde">Index</a> in_size, out_size;</div>
<div class="line">};</div>
<div class="line"> </div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#gaf5dfa8fd62427c5f3c8035daf15ebfcb">Matrix3i</a> A;</div>
<div class="line">A.reshaped() = <a class="code" href="classEigen_1_1DenseBase.html#a1c6d1dbfeb9f6491173a83eb44e14c1d">VectorXi::LinSpaced</a>(9,1,9);</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;Initial matrix A:\n&quot;</span> &lt;&lt; A &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line"><a class="code" href="group__matrixtypedefs.html#gaceba5f63d4040c79c44109590d4fa606">MatrixXi</a> B(5,5);</div>
<div class="line">B = A(pad{3,5}, pad{3,5});</div>
<div class="line">cout &lt;&lt; <span class="stringliteral">&quot;A(pad{3,N}, pad{3,N}):\n&quot;</span> &lt;&lt; B &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_a1c6d1dbfeb9f6491173a83eb44e14c1d"><div class="ttname"><a href="classEigen_1_1DenseBase.html#a1c6d1dbfeb9f6491173a83eb44e14c1d">Eigen::DenseBase::LinSpaced</a></div><div class="ttdeci">static EIGEN_DEPRECATED const RandomAccessLinSpacedReturnType LinSpaced(Sequential_t, Index size, const Scalar &amp;low, const Scalar &amp;high)</div><div class="ttdef"><b>Definition:</b> CwiseNullaryOp.h:246</div></div>
<div class="ttc" id="agroup__matrixtypedefs_html_gaf5dfa8fd62427c5f3c8035daf15ebfcb"><div class="ttname"><a href="group__matrixtypedefs.html#gaf5dfa8fd62427c5f3c8035daf15ebfcb">Eigen::Matrix3i</a></div><div class="ttdeci">Matrix&lt; int, 3, 3 &gt; Matrix3i</div><div class="ttdoc">3×3 matrix of type int.</div><div class="ttdef"><b>Definition:</b> Matrix.h:499</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">Initial matrix A:
1 4 7
2 5 8
3 6 9

A(pad{3,N}, pad{3,N}):
1 1 1 4 7
1 1 1 4 7
1 1 1 4 7
2 2 2 5 8
3 3 3 6 9

</pre> </td></tr>
</table>
<p><br  />
 </p>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
